import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import 'normalize.css'
import App from './App'
import router from './router'
import request from '@/common/js/request'
import axios  from 'axios'
import store from './vuex/store'
import { getToken } from './util/token'
import {getMenu} from '@/util/auth'

Vue.use(ElementUI, { size: 'small', zIndex: 3000 });
Vue.config.productionTip = false;
Vue.prototype.$http = request;
Vue.prototype.$axios = axios;

router.beforeEach((to, from, next) => {
  // console.log(getToken())    
  if (!getToken()) {   //token不存在
    if (to.matched.length > 0 ) {
      console.log(to.matched)
        next()
    } else {
        next({ path: '/login' })
    }
  } else {  //token 存在
    if (store.state.permission.addRoutes.length === 0) {
      store.dispatch('GenerateRoutes').then(() => {
        next({ path: to.path })
      });
    } else {
        if (to.path !== '/login') {
            next()
        } else {
            next(from.fullPath)
        }
    }
  }
  // if (to.path === '/login') {
  //   next();
  // } else {
  //   let token = getToken();
  //   if (token) {
  //     next('/login');
  //   } else {
  //     next();
  //   }
  // }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
